import requests
import json
import execjs

headers = {
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
    "cache-control": "no-cache",
    "content-type": "application/json;charset=UTF-8",
    "origin": "https://www.xiaohongshu.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://www.xiaohongshu.com/",
    "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
    "x-b3-traceid": "90ac02f975f6e293",
    # "x-s": "XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6ImVjMjIzNmU4ZDgyNTRkYzQzOTQ2M2UwZWYyNmJjMTg0ZGIwODBiNzY1NjVkNWJiMmY5NjM1ZTE2N2Y4ZGRlMjg4ZmZhYThmOGYwMjk4ZTQ2MjQyYjQ4MjQzY2E5ODgwNDZhYTUzMzBmMjVlODM2YjM3YjcyZWQ2M2I1ZjE3NjE0OTM5YWZkMGJkZDA2ZTEyZGEwMzE1OGI4YTc4NmE0ZDJkZjA2YTU5ZTU1MjAzOTUxOTc2MzJkMWNjYWU4NGUzNTY4MGY3ZDUwMGVlOGU1MDJhN2JkM2Y1OTNkMGMzY2I0MTU4ODcxZTAxMzI2Mzk1NjQ2NTk3YjlkNWI1NjkwYTc2MjViMGY1NDRmYWI4YjBiMTJmYjZkZGQ2MzFmNTI4NDkyY2Y2NTcyNzkxZmRjZjBmNjVhYjA0ZTM0ODRkYTAwZGM2M2FhMzIyZWUxMWM0ZjE5N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SpsRiJFS9+9pr8DTSzobFnrSh+d+h4g4p+Bpz4rSbzsTQ404A2rSwq7Ym87PIGA4A8bm7yLS9ab4Q4DSBGMm7nDSeapQQyB4ApDIFJrExad+fqgzFanYIaBMM4BWFaLTA8rGM8n8l47mCpd4daLP3zrSkagS0Lo4Uqpm7aFSePBpD/emA2bmFt7SM4rpQyAzTHjIj2eDjw0rMwerh+eP9+AHVHdWlPsHCP/QR",
    # "x-t": "1734331842723",
    "x-xray-traceid": "c9e71de04d295de43fdc537618ef0efd"
}
cookies = {
    "abRequestId": "e680cf11-30ff-5c61-80b2-477c37314791",
    "xsecappid": "xhs-pc-web",
    "a1": "1923c8e8ec93qax9g045ss76sq5l1vu3bbhiufh8c50000214839",
    "webId": "30b4cbde16de5fa9607d1ef6d2db2244",
    "web_session": "030037a1cbad0f1c2ec630bf80214a2ca1503a",
    "gid": "yjJqSYdj8JDdyjJqSYdYdDixSjqA0Cjk842MxYSMWKMA23281yh7q7888Jy4Yqj8SSDJKj28",
    "webBuild": "4.47.1",
    "acw_tc": "0a4aaa8d17343318237596780e115f673c34ab99a74e347d48f66d21427255",
    "unread": "{%22ub%22:%22674004a80000000007030c85%22%2C%22ue%22:%2267425bd90000000006016123%22%2C%22uc%22:25}",
    "websectiga": "634d3ad75ffb42a2ade2c5e1705a73c845837578aeb31ba0e442d75c648da36a",
    "sec_poison_id": "3917867d-f065-4bc6-9756-14123ad1c439"
}
url = "https://edith.xiaohongshu.com/api/sns/web/v1/homefeed"
data = {
    "cursor_score": "14548",
    "num": 31,
    "refresh_type": 3,
    "note_index": 111,
    "unread_begin_note_id": "",
    "unread_end_note_id": "",
    "unread_note_count": 0,
    "category": "homefeed.household_product_v3",
    "search_key": "",
    "need_num": 6,
    "image_formats": [
        "jpg",
        "webp",
        "avif"
    ],
    "need_filter_image": False
}

sign = execjs.compile(open('xhs.js', 'r', encoding='utf-8').read()).call('main',data)
headers['x-s'] = sign['X-s']
headers['x-t'] = str(sign['X-t'])


data = json.dumps(data, separators=(',', ':'))
response = requests.post(url, headers=headers, cookies=cookies, data=data)

print(response.text)
print(response)